package com.hitalkie.teacher.util;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.preference.PreferenceManager;

import java.lang.ref.WeakReference;
import java.util.ArrayList;

/**
 * Preferences控制的简单封装
 *
 * @author ID_MARR
 */
public class PreferencesController {

    private SharedPreferences mSharedPreferences;

    private SharedPreferences.Editor mEditor;

    private static PreferencesController mPreferences;

    private ArrayList<WeakReference<OnSharedPreferenceChangeListener>> mListeners = new ArrayList<WeakReference<OnSharedPreferenceChangeListener>>();

    /**
     * 构造方法 获取到应用的默认SharedPreferences 和它的Editor
     */
    private PreferencesController(Context context) {
        mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
        mEditor = mSharedPreferences.edit();
    }

    /**
     * 初始化
     */
    public static void init(Context context){
        mPreferences = new PreferencesController(context);
    }

    /**
     * 单例的获取
     */
    public static synchronized PreferencesController getInstance() {
        if (mPreferences == null) {
            throw new ExceptionInInitializerError("need call init first");
        }
        return mPreferences;
    }

    /**
     * 在Preferences储存一个String
     */
    public String getString(String key) {
        return mSharedPreferences.getString(key, "");
    }

    /**
     * 取出一个Preferences中储存的String
     */
    public void setString(String key, String value) {
        mEditor.putString(key, value);
        mEditor.commit();
    }

    /**
     * 在Preferences储存一个boolean
     */
    public boolean getBoolean(String key) {
        return mSharedPreferences.getBoolean(key, false);
    }

    /**
     * 取出一个Preferences中储存的boolean
     */
    public void setBoolean(String key, boolean value) {
        mEditor.putBoolean(key, value);
        mEditor.commit();
    }

    /**
     * 在Preferences储存一个int
     */
    public int getInt(String key) {
        return mSharedPreferences.getInt(key, 0);
    }

    /**
     * 取出一个Preferences中储存的int
     */
    public void setInt(String key, int value) {
        mEditor.putInt(key, value);
        mEditor.commit();
    }


    public synchronized boolean addListener(OnSharedPreferenceChangeListener listener) {
        boolean ret = false;
        if (listener != null) {
            WeakReference<OnSharedPreferenceChangeListener> ref = new WeakReference<OnSharedPreferenceChangeListener>(
                    listener);
            if (!mListeners.contains(ref)) {
                ret = mListeners.add(ref);
            }
            mSharedPreferences.registerOnSharedPreferenceChangeListener(listener);
        }
        return ret;
    }

    public synchronized boolean removeListener(OnSharedPreferenceChangeListener listener) {
        boolean ret = false;
        if (listener != null) {
            WeakReference<OnSharedPreferenceChangeListener> ref = new WeakReference<OnSharedPreferenceChangeListener>(
                    listener);
            ret = mListeners.remove(ref);
        }
        mSharedPreferences.unregisterOnSharedPreferenceChangeListener(listener);
        return ret;
    }

    public void setSharePreferencesListener(OnSharedPreferenceChangeListener listener) {
        mSharedPreferences.registerOnSharedPreferenceChangeListener(listener);
    }

    public void delPreferencesListener(OnSharedPreferenceChangeListener listener) {
        mSharedPreferences.unregisterOnSharedPreferenceChangeListener(listener);
    }


}
